import {
    createSlice
} from "@reduxjs/toolkit";
import { useSelector } from "react-redux";
export type TTabs = {
    label: string,
    key: string,
    closable: boolean
}
const items: TTabs[] = [
    {
        label: "主页",
        key: "/",
        closable: false
    }
]

const commonSlice = createSlice({
    name: "common",
    initialState: {
        items,
        loading: false,
        pageInfo: {
            current: 1,// 当前页码
            pageSize: 1,// 每页显示的条数
            total: 1,//总条数
        }
    },
    reducers: {
        upLoading(state, { payload }) {
            state.loading = payload;
        },
        upPageInfo(state, { payload }) {
            state.pageInfo = payload;
        },
        addItems(state, { payload }) {
            if (!state.items.find((item: TTabs) => item.key === payload.key))
                state.items.push(payload);
        },
        // 1- 增加修改标题方法
        upItems(state, { payload }) {
            // payload:{key:xxxxx,label:医院名称}
            const info = state.items.find((v: any) => v.key === payload.key) as any;
            info.label = payload.label;
        },
        // delItemsByKey(state,{payload}){
        //     const index = state.items.findIndex((item:TTabs)=>item.key === payload);
        //     state.items.splice(index,1);
        // },
        delItemsByIndex(state, { payload }) {
            state.items.splice(payload, 1);
        }
    }
})
export const { addItems, upPageInfo, upItems, upLoading, delItemsByIndex } = commonSlice.actions;
export const useSelectorCommon = () => useSelector((state: any) => state.common);
export default commonSlice.reducer;